<template>
    <div class="e-form">
        <BillTop title="配置计划新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData.planConfiguration" :rules="rules" label-width="200px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="计划日期：" prop="billDate">
                            <el-date-picker
                                v-model="formData.planConfiguration.billDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="填报人：" prop="founderName">
                            <span>{{ formData.planConfiguration.founderName }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="币种：" prop="currencyId">
                            <el-select
                                v-model="formData.planConfiguration.currencyId"
                                placeholder="请选择"
                                @change="currencyChange"
                            >
                                <el-option
                                    v-for="(
                                        item
                                    ) in currencyForm"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="总计划金额（元）：" >
                            <span>{{ formData.planConfiguration.planAmount }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="长租计划金额（元）：" prop="longPlanAmount">
                            <span>{{ formData.planConfiguration.longPlanAmount }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="临租总金额（元）：">
                            <el-input
                                placeholder="请输入金额"
                                v-model="formData.planConfiguration.tempLeaseAmount"
                                @input="onAmount"
                                v-enter-float="{
                                    set: formData,
                                    val: 'planConfiguration',
                                    key: 'tempLeaseAmount'
                                }"
                                clearable>
                            </el-input>
                        </el-form-item>
                    </el-col>
                <el-col :span="12">
                        <el-form-item label="临租审核总金额（元）：">
                            <el-input
                                placeholder="请输入金额"
                                v-model="formData.planConfiguration.tempLeaseAuditAmount"
                                disabled
                                clearable>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="机构："  prop="orgName">
                                <span>{{ userInfo.orgInfo.orgName }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态：" prop="state">
                            <span>{{ statusLable(formData.planConfiguration.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item  label="备注：">
                        <el-input type="textarea" v-model="formData.planConfiguration.remarks"></el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="本位币：" prop="baseCurName">
                            <span>{{ formData.planConfiguration.baseCurName }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="人民币汇率：">
                            <span>{{formData.planConfiguration.rmbRate}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row v-if="showBaseCurName">
                    <el-col :span="12">
                        <el-form-item label="本位币汇率：" prop="baseCurRate">
                            <el-input
                                v-model="formData.planConfiguration.baseCurRate"
                                v-enter-float2="{
                                    set: formData,
                                    val: 'planConfiguration',
                                    key: 'baseCurRate'
                                }"
                                placeholder="请输入内容"
                                :disabled="!showBaseCurName"
                                clearable>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose"  v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import '@/utils/jquery.scrollTo.min'
import request from '@/api/equip/equipPlan.js'
import requestAudit from '@/api/equip/common.js'
export default {
    data () {
        return {
            tabsName: 'baseInfo',
            selectEquipVisible: false,
            formData: {
                //配置计划基础信息
                planConfiguration: {
                    'auditTime': '',
                    'auditor': '',
                    'auditorId': '',
                    'baseCurId': '',
                    'baseCurName': '',
                    'baseCurRate': null,
                    'baseCurTempLeaseAmount': 0,
                    'billDate': '',
                    'billId': '',
                    'billNo': '',
                    'changeBillId': '',
                    'changeBillNo': '',
                    'changeLongPlanAmount': 0,
                    'changeState': '',
                    'changeTempLeaseAmount': 0,
                    'changeTempLeaseAuditAmount': 0,
                    'currency': '',
                    'currencyId': '',
                    'founderId': '',
                    'founderName': '',
                    'longPlanAmount': 0,
                    'modifier': '',
                    'modifierId': '',
                    'nullifyCreated': '',
                    'nullifyCreator': '',
                    'nullifyCreatorId': '',
                    'nullifyDesciption': '',
                    'nullifyReason': '',
                    'orgId': '',
                    'orgName': '',
                    'planAmount': 0,
                    'recorder': '',
                    'recorderId': '',
                    'remarks': '',
                    'rmbId': '',
                    'rmbName': '',
                    'rmbRate': null,
                    'rmbSumTempLeaseAmount': 0,
                    'state': 0,
                    'tempLeaseAmount': null,
                    'tempLeaseAuditAmount': null,
                    'version': 0,
                    'workId': ''
                },
                state: 0, //0只保存1保存并提交
            },
            rules: {
                billDate: [
                    { required: true, message: '请选择日期', trigger: 'blur' },
                ],
                currencyId: [
                    { required: true, message: '请选择币种', trigger: 'change'  },
                ],
                baseCurName: [
                    { required: true, message: '请输入本位币', trigger: 'blur' },
                ],
                baseCurRate: [
                    { required: true, message: '请输入本位币汇率', trigger: 'change' },
                    { validator: (rule, value, callback)=>{
                        if(value == 0) {
                            callback(new Error('本位币汇率必须大于0'))
                        }else{
                            callback()
                        }
                    }, trigger: 'change' }
                ]
            },
            showBaseCurName: false
        }
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            currencyForm: state => state.equip.equipData.currencyForm//币种
        }),
        //状态
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return (obj ?  obj.label :  '')
            }
        },
    },
    components: {
    },
    created () {
        this.getInfo()
    },
    mounted () {

    },
    methods: {
        getInfo () { //默认获取数据
            this.formData.planConfiguration.state = 0
            const date = new Date()
            this.formData.planConfiguration.billDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
            const { baseCyByOrgId } = this.userInfo
            this.formData.planConfiguration.baseCurName = baseCyByOrgId.currencyName  //本位币
            this.formData.planConfiguration.baseCurId = baseCyByOrgId.currency
            this.formData.planConfiguration.founderName = localStorage.getItem('userName')
        },
        onAmount () {
            this.formData.planConfiguration.tempLeaseAuditAmount = this.formData.planConfiguration.tempLeaseAmount
        },
        //币种切换
        currencyChange (val) {
            this.formData.planConfiguration.currency = this.currencyForm.find(x=>x.value === val).label  //币种
            requestAudit.getRmbRate({  //获取机构下某币种的人民币汇率
                currency: val,
                orgId: this.userInfo.orgInfo.orgId
            }).then(res=>{
                if(res.rate === 1) {
                    this.formData.planConfiguration.rmbId = res.currency
                    this.formData.planConfiguration.rmbRate = res.rate
                }else{
                    this.formData.planConfiguration.rmbId = res.rateId
                    this.formData.planConfiguration.rmbRate = res.rate
                }
            }).catch(()=>{

            })
            if(this.formData.planConfiguration.currencyId === this.formData.planConfiguration.baseCurId) {   //币种=本位币
                this.formData.planConfiguration.baseCurRate = 1
                this.showBaseCurName = false
            }else{
                this.formData.planConfiguration.baseCurRate = null
                this.showBaseCurName = true
            }
        },
        //保存
        preserve () {
            this.formData.state = 0
            this.$refs.form.validate(valid => {
                if (valid) {
                    request.configPlanAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/configPlanListEdit',
                                query: {
                                    billid: res,
                                    name: 'planDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                } else {
                    return false
                }
            })
        },
        //取消
        handleClose () {
            this.$router.replace('/configPlanList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
